package com.example.newspublish.config;

import com.example.newspublish.exception.MyAccessDeniedHandler;
import com.example.newspublish.exception.MyAuthenticationEntryPoint;
import com.example.newspublish.filter.AuthenticationFilter;
import com.example.newspublish.service.JWTUserDetailsService;
import com.example.newspublish.util.JwtTokenManager;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
@RequiredArgsConstructor
public class SecurityConfig {
    private final MyAuthenticationEntryPoint myAuthenticationEntryPoint;
    private final MyAccessDeniedHandler myAccessDeniedHandler;
    private final JWTUserDetailsService jwtUserDetailsService;
    private final JwtTokenManager jwtTokenManager;
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeHttpRequests()
                .anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(myAuthenticationEntryPoint)
                .accessDeniedHandler(myAccessDeniedHandler)
        ;
        // disable cache
        http.headers().cacheControl();
        http.addFilterBefore(new AuthenticationFilter(jwtTokenManager), UsernamePasswordAuthenticationFilter.class);
//        http.addFilterAt(new EmailAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }

    //   忽略鉴权
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring()
                .requestMatchers(
                        "/doc.html",
                        "/swagger-resources/**",
                        "/v3/api-docs/**",
                        "/swagger-ui/**",
                        "/ws/**",
                        "/favicon.ico",
                        "/webjars/**",
                        "/sysUser/user/login/**",
                        "/sysUser/user/logout/**",
                        "/user/register",
                        "/category/getAllCategory",
                        "/news/getAllList",
                        "/news/getAllList",
                        "/news/getListByCategoryId/**",
                        "/news/getDetailById/**",
                        "/news/searchNews",
                        "/news/page",
                        "/news/getNewsByUserName",
                        "/comment/commentList",
                        "/comment/getListByPage",
                        "/files/upload"
                );
    }


    @Autowired
    private AuthenticationConfiguration authenticationConfiguration;

    @Bean
    public AuthenticationManager authManager(HttpSecurity http) throws Exception {
        AuthenticationManagerBuilder authenticationManagerBuilder =
                http.getSharedObject(AuthenticationManagerBuilder.class);
        authenticationManagerBuilder.authenticationProvider(authProvider());//原来默认的
        return authenticationManagerBuilder.build();
    }

    @Bean
    public DaoAuthenticationProvider authProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(jwtUserDetailsService);
        return authenticationProvider;
    }
}

